<?php

App::uses('AppModel', 'Model');
App::uses('CompositeKeyModel', 'Model');

/**
 * OrganisationGroup Model
 *
 * @property OrganisationGroupType $OrganisationGroupType
 * @property OrganisationMemberGroup $OrganisationMemberGroup
 */
class AccountRole extends CompositeKeyModel {

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    public $useDbConfig = "organisations";

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'OrganisationAccount' => array(
            'className' => 'OrganisationAccount',
            'foreignKey' => 'account_role_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public function createDefault($organisation_id) {
        $roles = array(
            array(
                'organisation_id' => $organisation_id,
                'name' => 'Administrator',
                'description' => 'Overall administrator with all account privileges'
            ),
            array(
                'organisation_id' => $organisation_id,
                'name' => 'Data Manager',
                'description' => 'Manage member information and basic system settings'
            )
        );
        $this->saveAll($roles);

        $role_data = $this->find('first',array(
            'conditions'=>array('organisation_id' => $organisation_id),
            'recursive'=>-1
        ));

        if($role_data){
            $role_id = $role_data['OrganisationRole']['id'];
        }
        else{
            $role_id = false;
        }
        
        return $role_id;
    }
}
